/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.brands.application.ics.delegate;

import br.com.brands.application.ics.beans.ProductsBeans;
import br.com.brands.application.ics.factory.DAOFactory;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

/**
 *
 * @author ppacheco
 */
public class ProductsDelegate implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -7190972381493944101L;
    private static ProductsDelegate instance = null;
    private static final Logger log = Logger.getLogger(ProductsDelegate.class.getName());

    public ProductsDelegate() {
        log.log(Level.INFO, "Loading ProductsDelegate.");
    }

    public ProductsBeans save(ProductsBeans products) throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (products != null) {
                DAOFactory.getInstance().getProductsDAOImpl().save(products);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return products;
    }

    public ProductsBeans saveOrUpdate(ProductsBeans products)
            throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (products != null) {
                DAOFactory.getInstance().getProductsDAOImpl().saveOrUpdate(products);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return products;
    }

    public ProductsBeans delete(ProductsBeans products)
            throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (products != null) {
                DAOFactory.getInstance().getProductsDAOImpl().delete(products);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return products;
    }

    public ProductsBeans findByID(ProductsBeans products, Long id, boolean lock) throws HibernateException, InstantiationException {

        ProductsBeans objRef = null;

        try {
            if (id != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                objRef = (ProductsBeans) DAOFactory.getInstance().getProductsDAOImpl().findByID(products, id, lock);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return objRef;
    }

    @SuppressWarnings("unchecked")
    public List<ProductsBeans> findAll(ProductsBeans products) throws HibernateException, InstantiationException {

        List<ProductsBeans> list = null;

        try {
            if (products != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                list = DAOFactory.getInstance().getProductsDAOImpl().findAll(products);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return list;
    }

    public static ProductsDelegate getInstance() throws InstantiationException {
        try {
            if (instance == null) {
                instance = new ProductsDelegate();
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return instance;
    }
}
